package com.binguo.service;

import com.binguo.common.enums.FileUpload;
import com.binguo.common.util.MultipartFileUtil;
import com.binguo.model.mapper.CashierDeskMapper;
import com.binguo.model.mapper.DeviceLogMapper;
import com.binguo.model.model.CashierDesk;
import com.binguo.model.model.DeviceLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Service
public class LogSaveService {

    private static final Logger logger = LoggerFactory.getLogger(LogSaveService.class);

    @Resource
    private DeviceLogMapper deviceLogMapper;
    @Resource
    private CashierDeskMapper cashierDeskMapper;

    public FileUpload UploadLogFile(MultipartFile file, Integer deviceId){
        if(Objects.isNull(file) ||  null == deviceId  || file.isEmpty()){
            return FileUpload.NULL_FILE;
        }else{
            logger.info("进入UploadLogFile方法，携带参数file:"+file+",+","deviceId:"+deviceId );
        }
        String fileUrl = MultipartFileUtil.uploadFile(file);
        if(null == fileUrl){
            return FileUpload.SAVE_ERROR;
        }
        CashierDesk cashierDesk = cashierDeskMapper.selectByPrimaryKey(deviceId);
        if(null == cashierDesk){
            return FileUpload.DEVICE_ERROR;
        }
        DeviceLog deviceLog = DeviceLog.builder().id(0).deviceId(deviceId).
                logUrl(fileUrl).shopId(cashierDesk.getShopid()).branchId(cashierDesk.getBranchid()).
                logType(true).updateTime(new Date()).fileName(file.getOriginalFilename()).build();

        int num = deviceLogMapper.insertSelective(deviceLog);
        if(num < 0){
            return FileUpload.INSERT_ERROR;
        }
        return FileUpload.SUCCESS;
    }



}
